package com.boot.project.core.config;

import com.boot.project.core.interceptor.PermissionRequiredInterceptor;
import com.boot.project.core.interceptor.ResponseResultInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Autowired
    private ResponseResultInterceptor responseResultInterceptor;

    @Autowired
    private PermissionRequiredInterceptor permissionRequiredInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        List<String> staticResource = new ArrayList<>();
        staticResource.add("/file/**");
        staticResource.add("/css/**");
        staticResource.add("/js/**");
        staticResource.add("/fonts/**");
        staticResource.add("/image/**");
        staticResource.add("/*.ico");
        staticResource.add("*.html");
        staticResource.add("/**/*.html");

        // 1.注册ResponseResultInterceptor
        registry.addInterceptor(responseResultInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(staticResource);

        // 2.注册PermissionRequiredInterceptor
        registry.addInterceptor(permissionRequiredInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(staticResource);

    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        converters.add(new MappingJackson2HttpMessageConverter());
    }

}

